package com.flyme.module.base.provider.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.api.SysLecturerRemoteService;
import com.flyme.module.base.client.entity.SysLecturer;
import com.flyme.module.base.client.entity.SysUser;
import com.flyme.module.base.provider.service.SysLecturerService;
import com.flyme.module.base.provider.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 讲师管理
 *
 * @author zyf
 */
@Api(tags = "讲师管理")
@RestController
public class SysLecturerController extends BaseController<SysLecturerService, SysLecturer> implements SysLecturerRemoteService {

    @Autowired
    private SysLecturerService sysLecturerService;
    @Autowired
    private SysUserService sysUserService;


    /**
     * 讲师列表
     *
     * @return
     */
    @ApiOperation(value = "讲师列表", notes = "讲师列表")
    @Override
    public ResultBody list(@ModelAttribute PagerInfo pagerInfo, @ModelAttribute SysLecturer sysLecturer,String mobile) {
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("l.id", "user.userName", "user.avatar","user.email","user.mobile","l.summary", "user.age", "user.sex", "user.createTime", "lecturerName", "email","isShow",
            "(select count(c.id) from sys_course c where c.lecturerId = l.id) cardNo");
        cq.like("lecturerName", sysLecturer.getLecturerName());
        cq.like("user.mobile",mobile);
        cq.gt("user.createTime",sysLecturer.getStartDate());
        cq.lt("user.createTime",sysLecturer.getEndDate());
        cq.eq("user.userType","UT_LECTURER");
        cq.createAlias("user", "user");
        PqGrid pqGrid = sysLecturerService.pageList(cq);
        return ResultBody.success(pqGrid);
    }

    @ApiOperation(value = "添加讲师", notes = "添加讲师")
    @Override
    public ResultBody addLecturer(@ModelAttribute SysUser sysUser,String content, String summary) {
        Boolean tag = sysLecturerService.addLecturer(content,sysUser,summary);
        return ResultBody.success("添加成功").setData(tag);
    }

    @ApiOperation(value = "更新讲师", notes = "更新讲师")
    @Override
    public ResultBody updateLecturer(@ModelAttribute SysUser sysUser,String content, String summary) {
        Boolean tag = sysLecturerService.updateLecturer(content,sysUser,summary);
        return ResultBody.success("更新成功").setData(tag);
    }

    @ApiOperation(value = "删除讲师", notes = "删除讲师")
    @Override
    public ResultBody delByIds(@RequestParam Long[] ids) {
        Boolean tag = sysLecturerService.delByIds(ids);
        return ResultBody.success(tag);
    }

    @Override
    public ResultBody getLecturer(@PathVariable Long id) {
        SysUser user = sysUserService.getById(id);
        SysLecturer lecturer = sysLecturerService.getById(id);
        Map<String,Object> userMap = BeanUtil.beanToMap(user);
        Map<String,Object> lecturerMap = BeanUtil.beanToMap(lecturer);
        userMap.putAll(lecturerMap);
        return ResultBody.success(userMap);
    }
    /**
     * 更新推荐状态
     *
     * @param id     教师ID
     * @param isShow 是否推荐
     * @return
     */
    @ApiOperation(value = "更新讲师推荐状态", notes = "更新讲师推荐状态")
    @Override
    public ResultBody setIsShow(@RequestParam("id") Long id, @RequestParam("isShow") Integer isShow) {
        boolean result = bizService.update(new UpdateWrapper<SysLecturer>().set("isShow", isShow).eq("id", id));
        return ResultBody.ok("设置成功").setData(result);
    }

}
